package com.lxl.bt.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.lxl.bt.R;
import com.lxl.bt.fragment.AbutUs1Fragment;
import com.lxl.bt.util.ACache;
import com.lxl.bt.view.CustomViewPager;
import com.yanzhenjie.sofia.Sofia;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by lvxinlong
 * on  关于我们
 * on  2021年3月7日20:57:13
 **/
public class AboutUsActivity extends BaseActivity {


    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.rlRight)
    RelativeLayout rlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tab_layout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.view_pager)
    CustomViewPager viewPager;
    private Context context;
    ACache aCache;
    FragmentAdapter fragmentAdapter;
    private int current;
    private String[] titles={"平台介绍","平台优势","矿场"};
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aboutus);
        ButterKnife.bind(this);
        //状态栏全透明全屏显示页面，布局能够延伸到状态栏和导航栏下方
        Sofia.with(this)
                .invasionStatusBar()
                .statusBarLightFont()
//                .statusBarDarkFont()
                .statusBarBackgroundAlpha(0)
                .invasionNavigationBar()
                .navigationBarBackgroundAlpha(0);

        context = this;
        rlBack.setVisibility(View.VISIBLE);
        txtToolbarTitle.setText("公司简介");
        aCache = ACache.get(this);
        refFragmentView();
    }
    void refFragmentView() {
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(fragmentAdapter);
        tabLayout.setViewPager(viewPager);
        viewPager.setCurrentItem(current);
        viewPager.setOffscreenPageLimit(titles.length);


        refTableTitleView();
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                current = position;
                refTableTitleView();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private void refTableTitleView() {
        for (int i = 0; i < titles.length; i++) {
            if (i == current) {
                tabLayout.getTitleView(current).getPaint().setFakeBoldText(true);
            } else {
                tabLayout.getTitleView(i).getPaint().setFakeBoldText(false);
            }
        }


    }


    @OnClick(R.id.rlBack)
    public void onClick() {
        finish();
    }


    private class FragmentAdapter  extends FragmentStatePagerAdapter {
//        private String[] titles;
        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return AbutUs1Fragment.newInstance(position);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }
    }
}
